﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class loginAction : CWebBase
{
    public String jsonStr = "";
    public Hashtable json = new Hashtable();
    public CDatabase _db = new CDatabase();

    protected void Page_Load(object sender, EventArgs e)
    {
        _db.GetConnection();

        string message = "";
        string mode = CheckNull(Request["mode"]);

        string employee_num = CheckNull(Request["_user_id"]);
        string user_pwd = CheckNull(Request["_user_password"]);

        Boolean ssoMode = mode == "2" ? true : false;

        // sso 접근
        if (!ssoMode)
        {
            user_pwd = EncodeMd5(user_pwd);
        }
        else
        {
            string today = EncodeMd5(DateTime.Now.ToString("yyyy-MM-dd"));
            string qs = CheckNull(Request["qs"]);

            if (!isEmpty(qs))
            {
                qs = DecodeBase64(qs).Replace(today, "|");
                string[] arr = qs.Split('|');
                employee_num = arr[0];
                user_pwd = arr[1];
            }
        }

        if (employee_num == "" || user_pwd == "")
            ErrorBack("잘못된 경로로 접근 했습니다.");

        // 일반 로그인
        String userid = RemoveSpecialChars(employee_num);
        string sql = "Select UserId, UserPassword, UserName From ANALYZE_ADMIN Where UserId = '" + userid + "'";
        
        SqlDataReader dr = _db.GetQueryResult(sql);

        if (dr.HasRows)
        {
            if (dr.Read())
            {
                string id = dr["UserId"].ToString();
                string pw = dr["UserPassword"].ToString();
                string nm = dr["UserName"].ToString();

                // 패스워드 검사
                if (user_pwd.Equals(pw))
                {
                    message = "Y";
                    Session.Add("_Login_User_ID", id);
                    Session.Add("_Login_User_NAME", nm);
                }
                else {
                    message = "PW";
                }

                if (CheckNull(Request["redirecturl"]) != "")
                {
                    jsonStr += "reUrl:'" + Request["redirecturl"] + "', ";
                    json.Add("reUrl", Request["redirecturl"]);
                }
            }
        }
        else { 
            // 사번이 없는 경우
            message = "ID";
        }
        dr.Close();

        if (ssoMode) {
            JustGo("/analyze_new/admin/login.aspx");
        }

        json.Add("message", message);

        Response.AddHeader("'Content-type", "application/json");
        Response.Write(getJson(json));
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        _db.ReleaseConnection();
    }
    
}
